package com.ruoyi.web.sso.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.core.config.TokenConfig;
import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.common.util.BusinessException;
import com.ruoyi.web.sso.common.util.ErrorCode;
import com.ruoyi.web.sso.domain.DTO.StaffDTO;
import com.ruoyi.web.sso.domain.DTO.StaffPageDO;
import com.ruoyi.web.sso.domain.DTO.StatusResponse;
import com.ruoyi.web.sso.domain.DictData;
import com.ruoyi.web.sso.domain.Performance;
import com.ruoyi.web.sso.domain.PerformanceDetail;
import com.ruoyi.web.sso.domain.Staff;
import com.ruoyi.web.sso.domain.vo.RedisDetails;
import com.ruoyi.web.sso.service.IRedisUtilService;
import com.ruoyi.web.sso.service.IStaffService;
import com.ruoyi.web.sso.service.PerformanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 绩效考核管理
 *
 * */
@Controller
@RequestMapping("/api/sso/performance")
@Api(tags = "绩效考核管理")
public class PerformanceController extends BaseController
{

    @Autowired
    private PerformanceService performanceService;

    @Autowired
    private IRedisUtilService redisService;

    @Autowired
    private IStaffService staffService;


    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param year
     * @param staffName
     * @return
     */
    @PostMapping(value = "/performanceList", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "绩效考核列表查询", notes = "绩效考核列表查询")
    @ResponseBody
    @TokenConfig(value = "1")
    public StatusResponse getAreaStaffs(Integer pageNum, Integer pageSize,
                                        Integer year, String staffName,String areaId) {

        PageImpl<Performance> performances = performanceService.performanceList(pageNum, pageSize, year, staffName, areaId);

        return StatusResponse.success(performances);
    }

    /**
     * 详情
     * @param month
     * @param year
     * @param staffId
     * @return
     */
    @PostMapping(value = "/detail", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "详情", notes = "详情")
    @ResponseBody
    public StatusResponse detail(Integer month, Integer year, String staffId) {

        return StatusResponse.success(performanceService.detail(month,year,staffId));
    }


    @PostMapping(value = "/saveOrUpdatePerformance", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "员工入职", notes = "员工入职")
    @ResponseBody
    public StatusResponse saveOrUpdatePerformance(@RequestHeader String ticket,String performanceDetailStr) {
        RedisDetails redisDetails = redisService.getTicket(ticket);
        if (AssertUtil.isEmpty(redisDetails)){
            throw new BusinessException(ErrorCode.UNDEFINED, "ticket不存在");
        }
        Staff staff = staffService.selectStaffById(redisDetails.getStaffId());

        PerformanceDetail performanceDetail = new PerformanceDetail();

        JSONObject jsonObject = JSONObject.parseObject(performanceDetailStr);
        String id = jsonObject.getString("id");
        String staffId = jsonObject.getString("staffId");
        String performanceId = jsonObject.getString("performanceId");
        Integer year = jsonObject.getInteger("year");
        Integer month = jsonObject.getInteger("month");
        BigDecimal teachingAbility = jsonObject.getBigDecimal("teachingAbility");
        BigDecimal workingAttitude = jsonObject.getBigDecimal("workingAttitude");
        BigDecimal professional = jsonObject.getBigDecimal("professional");
        BigDecimal other = jsonObject.getBigDecimal("other");
        Integer parentalSatisfaction = jsonObject.getInteger("parentalSatisfaction");
        BigDecimal finalScore = jsonObject.getBigDecimal("finalScore");
        String comment = jsonObject.getString("comment");

        performanceDetail.setId(id);
        performanceDetail.setStaffId(staffId);
        performanceDetail.setPerformanceId(performanceId);
        performanceDetail.setYear(year);
        performanceDetail.setMonth(month);
        performanceDetail.setTeachingAbility(teachingAbility);
        performanceDetail.setWorkingAttitude(workingAttitude);
        performanceDetail.setProfessional(professional);
        performanceDetail.setOther(other);
        performanceDetail.setParentalSatisfaction(parentalSatisfaction);
        performanceDetail.setFinalScore(finalScore);
        performanceDetail.setComment(comment);


        return performanceService.saveOrUpdatePerformance(staff,performanceDetail);
    }


    /**
     * 删除
     * @return
     */
    @PostMapping(value = "/remove", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "删除", notes = "删除")
    @ResponseBody
    public StatusResponse remove(String id) {

        return performanceService.removePerformance(id);
    }


}
